package remove;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocal;
import org.multimedia.jolitorask.asterisk.AsteriskWrapperLocalHome;
import org.multimedia.jolitorask.security.AuthServlet;
import org.multimedia.jolitorask.telephone.TelephoneLocal;
import org.multimedia.jolitorask.telephone.TelephoneLocalHome;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocal;
import org.multimedia.jolitorask.utilisateur.UtilisateurLocalHome;

/**
 * Servlet implementation class for Servlet: RemoveUser
 *
 */
 public class RemoveUser extends AuthServlet implements javax.servlet.Servlet {
	 
		private static final long serialVersionUID = 1L;
		private static final String JNDI_NAME_WRAPPER = "asteriskWrapperSessionLocal";
		private static final String JNDI_NAME_TELEPHONE = "telephoneLocal";
		private static final String JNDI_NAME = "utilisateurLocal";
			
		
		public RemoveUser() {
			super();
		}   	
		
		@SuppressWarnings("unchecked")
		protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

			
			    res.setContentType("text/plain");
			    PrintWriter out = res.getWriter();
			    
			    String utilisateur;
			    String account;
			    
			    /* On recupere le nom */
			    if( (utilisateur=req.getParameter("login")) == null){
			    	out.println(false);
			    	return;
			    }
			    
			    /* On recupere le client */
			    if( (account=req.getParameter("account")) == null){
			    	out.println(false);
			    	return;
			    }
			   
			    if( !authentificate(req)){
			    	out.println(false);
			    	return;
			    }
			    
			    /* Creation */
			    Context initContext = null;
			    Hashtable env = new Hashtable(1);
			    Object beanObject = null;
			    
			    	
			    try {
			    	
					Collection<UtilisateurLocal> liste = client.getUtilisateurs();
					
					Iterator<UtilisateurLocal> it = liste.iterator();
					
					UtilisateurLocal user = null;
					while( it.hasNext() ){
						UtilisateurLocal u = it.next();
						
						if(u.getLogin().equals(utilisateur)){
							user = u;
							break;
						}
							
					}
					
					if( user == null){
						out.println(false);
						return;
					}
					
					Iterator<TelephoneLocal> iteTelephone = user.getTelephones().iterator();
					Vector<Integer> telPrimKey = new Vector<Integer>();
					while(iteTelephone.hasNext())
						telPrimKey.add((Integer)iteTelephone.next().getPrimaryKey());
					
					user.getTelephones().clear();
					
					beanObject = initContext.lookup(JNDI_NAME_TELEPHONE);
					TelephoneLocalHome homeTel = (TelephoneLocalHome)beanObject;
					Iterator<Integer> iterator = telPrimKey.iterator();
					while (iterator.hasNext()) {
						
						homeTel.remove(iterator.next());
					}
						
					
					initContext = new javax.naming.InitialContext(env);
			    	beanObject = initContext.lookup(JNDI_NAME);
			    	UtilisateurLocalHome home2 = (UtilisateurLocalHome) beanObject;
			    	int id_user  = ((Integer)user.getPrimaryKey()).intValue();
					home2.remove(user.getPrimaryKey());
					
					//Objet Asterisk
					beanObject = initContext.lookup(JNDI_NAME_WRAPPER);
					AsteriskWrapperLocalHome homeA = (AsteriskWrapperLocalHome)beanObject;
					AsteriskWrapperLocal wrapper = homeA.create();
					wrapper.init(account, utilisateur);
					wrapper.deleteUser(id_user);

					
				} catch (SQLException e1) {
					out.println(false);
					return;
				}
			    catch(NamingException e2){
			    	out.println(false);
			    	return;
			    }
			    catch (CreateException e) {
			    	out.println(false);
			    	return;
				}
			    catch (RemoveException e) {
			    	out.println(false);
			    	return;
				}
			    
			    out.println(true);
		}  	
		
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		}   	   	  	     	  	    
}